package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.http.org.entity.apply.OrgApplyCheYouRong;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

import java.util.Map;

/**
 *  车优融
 *
 *  https://apifox.com/apidoc/shared-54c131ef-dad3-44c0-a0dd-fac730651a1c
 *
 *  @author WJB
 */
@ApiPlan(productId = "38", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5CheYouRong implements ApiOrgPhoneMd5ConvertHandler {

    private static final String KEY = "YJA5F60ff5N23Rsk";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        return MapUtils.newHashMap(newMap -> newMap.put("phoneMD5", apiDataExchangeParameter.getPhoneMd5()));
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        OrgApplyCheYouRong data = new OrgApplyCheYouRong();

        data.setCusName(apiApplyParameter.getUserName());
        data.setPhone(apiApplyParameter.getPhone());
        //  不能包含市
        String city = apiApplyParameter.getCity();
        if (ValidParam.isNotNull(city)) {
            city = city.replace("市", "");
            data.setCityName(city);
        }
        data.setLaiyuan("安心分期");
        //  拓展判断信息
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        //  性别
        data.setSex(apiFieldConverter.getSexText());
        //  贷款金额
        data.setAmount(apiFieldConverter.getLoanAmountUnit());
        //  车产
        String car = ChooseEq.create()
                .when(apiApplyParameter.getCarInfo(), "1", "无车产")
                .when(apiApplyParameter.getCarInfo(), "2", "有车不抵押")
                .when(apiApplyParameter.getCarInfo(), "3", "有车可抵押")
                .when(apiApplyParameter.getCarInfo(), "4", "有车产")
                .end();
        data.setCarStatus(car);
        //  公积金
        data.setAccumulation(apiFieldConverter.hasProvidentFundToStr());
        //  社保
        data.setSocial(apiFieldConverter.hasSocialSecurityToStr());
        //  房产
        data.setHouseType(apiFieldConverter.hasRealEstateInfoToStr());
        //  收入
        String monthlySalary = ChooseEq.create()
                .when(apiApplyParameter.getMonthlySalary(), "1", "3000")
                .when(apiApplyParameter.getMonthlySalary(), "2", "8000")
                .when(apiApplyParameter.getMonthlySalary(), "3", "15000")
                .when(apiApplyParameter.getMonthlySalary(), "4", "30000")
                .when(apiApplyParameter.getMonthlySalary(), "5", "50000")
                .end();
        data.setIncomeMonth(monthlySalary);
        //  工资发放
        String salaryPayMethod = ChooseEq.create()
                .when(apiApplyParameter.getSalaryPayMethod(), "1", "银行卡")
                .when(apiApplyParameter.getSalaryPayMethod(), "2", "现金")
                .when(apiApplyParameter.getSalaryPayMethod(), "3", "自存")
                .end();
        data.setPayoffType(salaryPayMethod);
        //  保险
        String insurancePolicy = ChooseEq.create()
                .when(apiApplyParameter.getInsurancePolicy(), "1", "无保险保单")
                .when(apiApplyParameter.getInsurancePolicy(), "2", "缴纳未满1年")
                .when(apiApplyParameter.getInsurancePolicy(), "3", "缴纳1年以上")
                .when(apiApplyParameter.getInsurancePolicy(), "4", "缴纳未满1年")
                .end();
        data.setBaodanScop(insurancePolicy);
        //  信用卡
        data.setCreditCard(apiFieldConverter.hasCreditLimitToStr());

        AES ecb = AES.createEcb(KEY.getBytes());
        Map<Object, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("data", ecb.encryptToBase64Str(JsonUtils.toJson(data)));
        });
        return map;

    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
